﻿#region 命名空间

using System;
using System.Data;
using System.IO;
using System.Linq;
using WinnerCMS.BLL;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.Template
{
    [Access("263004")]
    public partial class Template : WebFactory.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList();
            }
        }

        private void BindList()
        {
            this.GV_List.EmptyDataTemplate = GetTemplate(this);

            var dtFiles = new DataTable();

            DataRow drFiles;


            dtFiles.Columns.Add(new DataColumn("ICO", Type.GetType("System.String")));
            dtFiles.Columns.Add(new DataColumn("Name", Type.GetType("System.String")));
            dtFiles.Columns.Add(new DataColumn("FullName", Type.GetType("System.String")));
            dtFiles.Columns.Add(new DataColumn("Size", Type.GetType("System.String")));

            dtFiles.Columns.Add(new DataColumn("LastWriteTime", Type.GetType("System.DateTime")));
            dtFiles.Columns.Add(new DataColumn("Edit", Type.GetType("System.String")));
            dtFiles.Columns.Add(new DataColumn("See", Type.GetType("System.String")));

            //针对当前目录建立目录引用对象
            string strDir;
            if (Request["dir"] == null)
            {
                strDir = Server.MapPath("~/Templates/" + TemplatesManage.GetUseing.TemplatePath);
            }
            else
            {
                strDir = Server.UrlDecode(Request["dir"]);
            }
            var dirInfo = new DirectoryInfo(strDir);
            //循环判断当前目录下的文件和目录

            foreach (var fsi in dirInfo.GetFileSystemInfos())
            {
                if (fsi is FileInfo)
                    continue;

                drFiles = dtFiles.NewRow();

                var di = fsi as DirectoryInfo;
                drFiles["ICO"] = "folder.gif";
                drFiles["Name"] = "<a href=\"Template.aspx?dir=" + Server.UrlEncode(di.FullName) + "\">" + di.Name +
                                  "</a>";
                drFiles["FullName"] = di.FullName;

                var l = di.GetFiles().Sum(f => f.Length);
                drFiles["Size"] = FileSize(Convert.ToDouble(l));

                drFiles["LastWriteTime"] = di.LastWriteTime;
                dtFiles.Rows.Add(drFiles);
            }
            foreach (var fsi in dirInfo.GetFileSystemInfos())
            {
                if (fsi is DirectoryInfo)
                    continue;

                drFiles = dtFiles.NewRow();
                var fi = fsi as FileInfo;

                drFiles["ICO"] = fi.Extension.Replace(".", "") + ".gif";

                drFiles["Name"] = fi.Name;
                drFiles["FullName"] = fi.FullName;

                //取得文件的大小
                drFiles["Size"] = FileSize(Convert.ToDouble(fi.Length));

                //取得文件的创建时间
                drFiles["LastWriteTime"] = fi.LastWriteTime;

                if (fsi.Extension.ToLower() == ".aspx")
                {
                    drFiles["Edit"] = "<a href=\"Template_Edit.aspx?FilePath=" + Server.UrlEncode(fi.FullName) +
                                      "\">文本编辑</a>";
                    drFiles["See"] = "<a href=\"" + fi.FullName.Replace(Server.MapPath("~/"), "/") +
                                     "\" target=_blank>模板查看</a>";
                }
                else
                {
                    drFiles["Edit"] = "";
                    drFiles["See"] = "";
                }

                dtFiles.Rows.Add(drFiles);
            }

            this.GV_List.DataSource = dtFiles;
            this.GV_List.DataBind();
        }

        private string FileSize(double fileSize)
        {
            if ((fileSize < 1024*1024))
            {
                return Math.Round((fileSize/1024), 2) + " KB";
            }
            return Math.Round(fileSize/(1024*1024), 2) + " MB";
        }
    }
}